package com.victor.bingwallpaper.util;

import com.victor.bingwallpaper.lisenter.DownloadFinishListener;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

public class DownUtil {
    private static final int FAILED = 0;
    private static final int SUCCEED = 1;
    private static final String DIR = "C:\\Users\\000\\Pictures";
    private static File file = new File("C:\\Users\\000\\Pictures");
    private static List<String> files = new Vector();
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public DownUtil() {
    }

    public static final File getFile() {
        return file;
    }

    public static final List<String> getFileList() {
        if (files.size() == 0) {
            File[] var0 = getFile().listFiles();
            int var1 = var0.length;

            for(int var2 = 0; var2 < var1; ++var2) {
                File file = var0[var2];
                if (file.getName().contains(".jpg")) {
                    files.add(file.getAbsolutePath());
                }
            }
        }

        return files;
    }

    public static void downPhoto(String name, String picAddress) {
        File[] allFiles = DownUtil.file.listFiles();
        List<File> files = new ArrayList();
        File[] var4 = allFiles;
        int var5 = allFiles.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            File file = var4[var6];
            if (file.isFile()) {
                files.add(file);
            }
        }

        if (name == null && name == "") {
            name = sdf.format(new Date()) + ".jpg";
        }

        if (!name.endsWith(".jpg")) {
            name = name + ".jpg";
        }

        String NAME = name;
        if (!files.stream().anyMatch((file) -> file.getName().equals(NAME))) {
            HttpUtils.download(picAddress, "C:\\Users\\000\\Pictures", name, new DownloadFinishListener() {
                public void finish() {
                    CommUtil.saveLog(1, "下载成功");
                }

                public void error(Exception ex) {
                    CommUtil.saveLog(0, "下载图片时异常： " + ex.toString());
                }
            });
        }
    }

    static {
        if (!file.exists()) {
            file.mkdir();
        }

        getFileList();
    }
}
